package com.dabao.controller.admin;

import com.dabao.bean.Progress;
import com.dabao.controller.BaseController;
import com.dabao.service.impl.RedisService;
import com.dabao.util.DateUtils;
import com.dabao.util.KeyConstant;
import com.dabao.util.web.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by card on 16/8/2.
 */
@Controller
@RequestMapping("/admin/progress")
public class ProgressController extends BaseController
{
    @Autowired
    private RedisService redisService;

    @RequestMapping("")
    public String index() {
        return "/admin/progress/list";
    }


    @RequestMapping("/list")
    @ResponseBody
    public String list(@RequestParam(value = "draw", required = false, defaultValue = "1")String draw,
                       @RequestParam(value = "start", required = false, defaultValue = "0") int pageNo,
                       @RequestParam(value = "length", required = false, defaultValue = "0") int pageSize) {

        Map<String, String> map = redisService.hgetAll(KeyConstant.KEY_MAP);
        List<Progress> progresses = new ArrayList<Progress>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            Progress progress = new Progress();
            progress.setTime(DateUtils.formatTime(new Date(Long.valueOf(entry.getKey()))));
            progress.setTotalCount(entry.getValue());

            Long left = redisService.llen(entry.getKey());
            if(left == null) {
                left = 0L;
            }
            progress.setLeftCount(left.toString());
            progresses.add(progress);
        }
        return ResponseUtils.listData(map.size(), progresses, draw);
    }

}
